package hhAcademicBowl;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.JTextField;

public class FinalJeopardy {
	
	HHAC manager;
	JFrame frame;
	String finalJeopardyQ;
	String finalJeopardyA;
	private JTextField txtRedWager;
	private JTextField txtBlackWager;
	
	/**
	 * Create the application.
	 */
	public FinalJeopardy(HHAC man) {
		manager = man;
	}

	/**
	 * Initialize the contents of the frame.
	 * @wbp.parser.entryPoint
	 */
	void initialize() {
		frame = new JFrame();
		frame.getContentPane().setBackground(ColorConstants.BG);
		frame.getContentPane().setLayout(null);
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		//Dimension screen = new Dimension(640,480);
		frame.setBounds(0,0,screen.width,screen.height);
		frame.setUndecorated(true); //maximized
		
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		JLabel lblTitle = new JLabel("HH Academic Bowl");
		lblTitle.setHorizontalAlignment(SwingConstants.LEFT);
		lblTitle.setFont(new Font("Impact", Font.PLAIN, 69));
		lblTitle.setBounds((frame.getWidth()-525)/2, 10, 525, 68); //title position
		frame.getContentPane().add(lblTitle);
		
		JTextArea qLabel = new JTextArea();
		qLabel.setWrapStyleWord(true);
		qLabel.setEditable(false);
		qLabel.setBackground(ColorConstants.BG);
		qLabel.setLineWrap(true);
		qLabel.setFont(new Font("Tahoma", Font.PLAIN, 65));
		
		int qWidth = screen.width / 2;
		qLabel.setBounds((screen.width-qWidth)/2, (lblTitle.getY() + lblTitle.getHeight() + 50), qWidth, (screen.height*2/3));
		frame.getContentPane().add(qLabel);
		
		
		//CONTROL PANEL------------------------------------------------------------------------------------------
		
				JPanel controlPanel = new JPanel();
				controlPanel.setBorder(null);
				controlPanel.setBackground(ColorConstants.BG);
				int contY = (qLabel.getY() + qLabel.getHeight() + 10);
				controlPanel.setBounds((screen.width-qWidth)/2, contY, qWidth, ((int) screen.getHeight() - contY - 20));
				frame.getContentPane().add(controlPanel);
				controlPanel.setLayout(null);
				
				//non-persistent score panel
				JPanel scorePanel = new JPanel();
				scorePanel.setBackground(ColorConstants.BG);
				scorePanel.setBounds((controlPanel.getWidth()-300)/2, 10, 300, controlPanel.getHeight()-20);
				controlPanel.add(scorePanel);
				scorePanel.setVisible(false);
				scorePanel.setLayout(null);
				
				
				JCheckBox addToRed = new JCheckBox("Add Points to Red");
				addToRed.setBackground(ColorConstants.BG);
				addToRed.setBounds(6, 7, 167, 15);
				scorePanel.add(addToRed);
				
				JCheckBox subFromRed = new JCheckBox("Subtract from Red");
				subFromRed.setBackground(ColorConstants.BG);
				subFromRed.setBounds(6, 23, 167, 15);
				scorePanel.add(subFromRed);
				
				JCheckBox addToBlack = new JCheckBox("Add Points to Black");
				addToBlack.setBackground(ColorConstants.BG);
				addToBlack.setBounds(6, 39, 167, 15);
				scorePanel.add(addToBlack);
				
				JCheckBox subFromBlack = new JCheckBox("Subtract from Black");
				subFromBlack.setBackground(ColorConstants.BG);
				subFromBlack.setBounds(6, 55, 167, 15);
				scorePanel.add(subFromBlack);
				
				
				JButton btnQA = new JButton("Show Question");
				btnQA.setFont(new Font("Tahoma", Font.PLAIN, 20));
				
				// Q-A button
				
				final JButton f_btnQA = btnQA;
				final JTextArea f_qLabel = qLabel;
				final String f_Question = finalJeopardyQ;
				final String f_Answer = finalJeopardyA;
				final JPanel f_scorePanel = scorePanel;
				btnQA.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent arg0) {
						if(f_btnQA.getText().equalsIgnoreCase("Show Question")){
							f_btnQA.setText("Show Answer");
							f_qLabel.setText(f_Question);
							playSound();
						}
						else if(f_btnQA.getText().equalsIgnoreCase("Show Answer")){
							f_btnQA.setText("End Game");
							f_qLabel.setText(f_Answer);
							f_scorePanel.setVisible(true);
							f_btnQA.setVisible(false);
						}
					}
				});
				btnQA.setBounds((controlPanel.getWidth()-225)/2, 10, 225, controlPanel.getHeight()-20);
				controlPanel.add(btnQA);
				
				JLabel lblRedWager = new JLabel("Red Wager");
				lblRedWager.setFont(new Font("Tahoma", Font.PLAIN, 22));
				lblRedWager.setBounds( ((btnQA.getX()-115)/2), 10, 115, 27);
				controlPanel.add(lblRedWager);
				
				txtRedWager = new JTextField();
				txtRedWager.setFont(new Font("Tahoma", Font.PLAIN, 22));
				txtRedWager.setHorizontalAlignment(SwingConstants.CENTER);
				txtRedWager.setBounds(((btnQA.getX()-115)/2), 43, 115, 33);
				controlPanel.add(txtRedWager);
				txtRedWager.setColumns(10);
				
				
				Integer offset = btnQA.getX() + btnQA.getWidth();
				Integer space = controlPanel.getWidth() - offset;
				
				JLabel lblBlackWager = new JLabel("Black Wager");
				lblBlackWager.setFont(new Font("Tahoma", Font.PLAIN, 22));
				lblBlackWager.setBounds(((space-127)/2)+offset, 10, 127, 27);
				
				controlPanel.add(lblBlackWager);
				
				txtBlackWager = new JTextField();
				txtBlackWager.setFont(new Font("Tahoma", Font.PLAIN, 22));
				txtBlackWager.setHorizontalAlignment(SwingConstants.CENTER);
				txtBlackWager.setBounds(((space-127)/2)+offset, 43, 127, 33);
				
				controlPanel.add(txtBlackWager);
				txtBlackWager.setColumns(10);
				
				// end button 
				
				final JCheckBox AR = addToRed;
				final JCheckBox SR = subFromRed;
				final JCheckBox AB = addToBlack;
				final JCheckBox SB = subFromBlack;
				final JTextField RW = txtRedWager;
				final JTextField BW = txtBlackWager;
				final JFrame fr = frame;
				JButton btnEndGame = new JButton("End Game");
				btnEndGame.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						int rv = Integer.parseInt(RW.getText());
						int bv = Integer.parseInt(BW.getText());
						if(AR.isSelected()){
							manager.addToRed(rv);
						}
						if(SR.isSelected()){
							manager.subFromRed(rv);
						}
						if(AB.isSelected()){
							manager.addToBlack(bv);
						}
						if(SB.isSelected()){
							manager.subFromBlack(bv);
						}
						fr.dispose();
						manager.endGame();
					}
				});
				btnEndGame.setFont(new Font("Tahoma", Font.PLAIN, 14));
				btnEndGame.setBounds(179, 7, 111, 67);
				scorePanel.add(btnEndGame);
				
	}

	public void setFinalJeopardyQ(String finalJeopardyQ) {
		this.finalJeopardyQ = finalJeopardyQ;
	}

	public void setFinalJeopardyA(String finalJeopardyA) {
		this.finalJeopardyA = finalJeopardyA;
	}

	private void playSound(){
	    try{
	        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("jeopaend.wav").getAbsoluteFile());
	        Clip clip = AudioSystem.getClip();
	        clip.open(audioInputStream);
	        clip.start();
	    }catch(Exception ex){
	        System.out.println("Error with playing sound.");
	        ex.printStackTrace();
	    }
	}
}
